package com.distributeredis.redis_springboot.springbootexcel02.readtest;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class ReadTest {
    String PATH = "H:\\LUA\\redis_springboot\\src\\main\\java\\com\\distributeredis\\redis_springboot\\springbootexcel02\\user9.xlsx";
    private  final Integer  BATCH_SIZE = 5;
    // 方式一
    @Test
    public void test() {
        EasyExcel.read(PATH, ReadVO.class, new ReadListener<ReadVO>() {
            List<ReadVO> list = new ArrayList<>();
            @Override
            public void invoke(ReadVO data, AnalysisContext analysisContext) {
                list.add(data);
                if(list.size() > BATCH_SIZE) {
                    System.out.println("保存数据到数据库");
                    System.out.println(list);
                    list = new ArrayList<>();
                }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("保存剩余的数据到数据库");
                System.out.println(list);
            }
            // 为了应对这种需求，对于有问题的数据记录下来，不影响后续数据的读取
            @Override
            public void onException(Exception exception, AnalysisContext context) throws Exception {
                System.out.println(exception);
            }
        }).sheet().headRowNumber(1).doRead();
    }
    // 方式二  简化形式
    @Test
    public void test1() {
        EasyExcel.read(PATH, ReadVO.class, new PageReadListener<ReadVO>(readVOS -> {
                    System.out.println("简化形式，使用lambda表达式，别人写好的");
                    System.out.println(readVOS);
        })
        ).sheet().doRead();
    }
}
